//
//  SecondViewController.m
//  练习11
//
//  Created by 张琦 on 15/11/21.
//  Copyright © 2015年 张琦. All rights reserved.
//

#import "SecondViewController.h"
#import "AFNetworkToll.h"
#import "ModelSecondHot.h"
#import "UIImageView+WebCache.h"
#import "CellSecondHotDetails.h"
#import "MusicPlay.h"
#import "PlayViewController.h"

#import <AVFoundation/AVFoundation.h>
#define SCR_WIDTH     [UIScreen mainScreen].bounds.size.width
#define SCR_HEIGHT    [UIScreen mainScreen].bounds.size.height

@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate, MyMusicPlayDelegate>
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UILabel *labelTitle;
@property (nonatomic, retain) UIImageView *image1;
@property (nonatomic, retain) UIView *viewHeader;
@property (nonatomic, assign) float lastNewY;
@property (nonatomic, retain) AVPlayer *player;

@property (nonatomic, assign) NSInteger currentIndex;

@end

@implementation SecondViewController

+ (NSMutableAttributedString *)mutableAttributedString:(NSString *)strSum color:(NSArray *)arrayColor font:(NSArray *)arrayFont
{
    NSArray *array = [strSum componentsSeparatedByString:@"\n"];
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:strSum];
    int length = 0;
    for (int i = 0; i < arrayColor.count; i++) {
        [att addAttribute:NSForegroundColorAttributeName value:[arrayColor objectAtIndex:i] range:NSMakeRange(length, [[array objectAtIndex:i] length])];
        [att addAttribute:NSFontAttributeName value:[arrayFont objectAtIndex:i] range:NSMakeRange(length, [[array objectAtIndex:i] length])];
        length += [[array objectAtIndex:i] length] + 1;
    }
    return att;
}


- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCR_WIDTH, SCR_HEIGHT - 64 - 60) style:UITableViewStylePlain];
    self.tableView.tintColor = [UIColor yellowColor];
    //    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.separatorColor = [UIColor redColor];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[CellSecondHotDetails class] forCellReuseIdentifier:@"CellSecondHotDetails"];
    self.tableView.backgroundColor = [UIColor clearColor];
    //    self.tableView.backgroundView = self.view1;
    self.viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCR_WIDTH, 150)];
    self.tableView.tableHeaderView.alpha = 0;
    self.tableView.tableHeaderView = self.viewHeader;
    
    
    self.labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 375, 100)];
    self.labelTitle.numberOfLines = 3;
    self.labelTitle.textColor = [UIColor whiteColor];
    self.labelTitle.backgroundColor = [UIColor clearColor];
    [self.viewHeader addSubview:self.labelTitle];
    
    
    

    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCR_WIDTH, 60)];
    viewHeader.backgroundColor = [UIColor blackColor];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(0 + 30, 15, 30 , 30);
    button1.backgroundColor = [UIColor clearColor];
    [viewHeader addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake( SCR_WIDTH / 4 + 30, 15, 30 , 30);
    button2.backgroundColor = [UIColor clearColor];
    [viewHeader addSubview:button2];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake( SCR_WIDTH / 2 + 30, 15, 30 , 30);
    button3.backgroundColor = [UIColor clearColor];
    [viewHeader addSubview:button3];
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    button4.frame = CGRectMake(SCR_WIDTH / 4 * 3 + 30,15, 30 , 30);
    button4.backgroundColor = [UIColor clearColor];
    
    [button1 setBackgroundImage:[UIImage imageNamed:@"S4"] forState:UIControlStateNormal];
    [button2 setBackgroundImage:[UIImage imageNamed:@"S3"] forState:UIControlStateNormal];
    [button3 setBackgroundImage:[UIImage imageNamed:@"S2"] forState:UIControlStateNormal];
    [button4 setBackgroundImage:[UIImage imageNamed:@"S1"] forState:UIControlStateNormal];

    
    [viewHeader addSubview:button4];
    return viewHeader;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ModelSecondHot *model = [self.array firstObject];
    return model.songs.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ModelSecondHot *model = [self.array firstObject];
    NSArray *array = model.songs;
    CellSecondHotDetails *cell = [tableView dequeueReusableCellWithIdentifier:@"CellSecondHotDetails"];
    cell.number = (int)indexPath.row + 1;
    cell.dic = [array objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CellSecondHotDetails *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectedBackgroundView = [[UIView alloc] init];
    
    
    ModelSecondHot *model = [self.array firstObject];

    NSArray *array = model.songs;
    self.currentIndex = indexPath.row;
    [PlayViewController sharePlayViewController].array = array;

    [PlayViewController sharePlayViewController].currentNumber = self.currentIndex;

}



- (void)createImageView
{
    self.image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 300)];
    [self.view addSubview:self.image1];
    [_image1 release];
}
- (void)loadData
{
    self.array = [NSMutableArray array];
    // 视频网址

    [AFNetworkToll getUrl:self.url body:nil response:ZHResponseStyleJSON requestheadFile:nil success:^(NSURLSessionDataTask *task, id result) {
        ModelSecondHot *model = [[ModelSecondHot alloc] initWithDic:result];
        [self.array addObject:model];
        
        [self.image1 sd_setImageWithURL:[NSURL URLWithString:[model.image objectForKey:@"pic"]]];
        self.labelTitle.attributedText = [SecondViewController mutableAttributedString:[NSString stringWithFormat:@"%@\n%@", model.title, model.desc] color:@[[UIColor whiteColor], [UIColor whiteColor]] font:@[[UIFont systemFontOfSize:30], [UIFont systemFontOfSize:17]]];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error === %@", error);
    }];
}



-(void)change{
    self.tableView.bounces = YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y <= -100) {
        self.tableView.bounces = NO;
        [self performSelector:@selector(change) withObject:self afterDelay:1];
    }
//    NSLog(@"%f",scrollView.contentOffset.y);
    if (scrollView == self.tableView) {
        float newY = scrollView.contentOffset.y;
        
//        NSLog(@"%lf", newY);
        if (newY >= 0) {
            [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"2"] forBarMetrics:UIBarMetricsDefault];
            self.navigationController.navigationBar.alpha = (newY - 0) / 150;
        }
        if (newY <= 20) {
            self.navigationController.navigationBar.alpha = 1;
            
            [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
        }
        if (newY > -100 && newY < 0) {
            self.image1.transform = CGAffineTransformMakeScale((0 - newY) / 1000 + 1, (0 - newY) / 1000 + 1);
        }
        
    }
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createImageView];
    
    [self createTableView];

    [self loadData];

    [MusicPlay shareMusicPlay].delegate = self;
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
